package com.trs.screen.demo;

/**
 * Created by tangrisheng on 2016/5/17.
 * dp,px,sp transition.
 */
public class DpiUtil {


    public static final int CHINESE = 0;
    public static final int NUMBER_OR_CHARACTER = 1;

    float density = 1;


    public DpiUtil(float density) {
        this.density = density;
    }

    /**
     * px->dp
     *
     * @param pxValue px
     * @return dp
     */
    public float px2dip(float pxValue) {
        return pxValue / density;
    }

    /**
     * dp->px
     *
     * @param dipValue dp
     * @return px
     */
    public float dip2px(float dipValue) {
        return dipValue * density;
    }

    /**
     * sp->px
     *
     * @param spValue sp
     * @param type    CHINESE or NUMBER_OR_CHARACTER
     * @return px
     */
    public float sp2px(float spValue, int type) {
        switch (type) {
            case CHINESE:
                return spValue * density;
            case NUMBER_OR_CHARACTER:
                return spValue * density * 10.0f / 18.0f;
            default:
                return spValue * density;
        }
    }
}
